Skip to content

Add shallow equality guards to MessageBlockStore to prevent unnecessary renders - #1222

Open
nordicnode wants to merge 2 commits into
CodebuffAI:mainfrom
nordicnode:perf-message-block-equality-guard
Open

Add shallow equality guards to MessageBlockStore to prevent unnecessary renders#1222
nordicnode wants to merge 2 commits into
CodebuffAI:mainfrom
nordicnode:perf-message-block-equality-guard

Conversation

@nordicnode

@nordicnode nordicnode commented Sep 2, 2026

Copy link
Copy Markdown

Add shallow equality guards to MessageBlockStore to prevent unnecessary renders

Summary

• In cli/src/state/message-block-store.ts, added shallow equality guards to setContext and setCallbacks to prevent state reference churn and eliminate spurious notifications to store subscribers and context consumers.
• Previously, setContext (triggered on every layout effect in <Chat />) drafted and replaced state.context with a new object reference even when all property values were identical. Because Zustand notifies store subscribers whenever a new state reference is created by Immer, this triggered re-render passes for any components and listeners consuming the store or context directly.
• In setContext, added a key-by-key shallow equality guard for merged properties.
• In setCallbacks, respected full replacement semantics by checking key set length before checking individual values, ensuring that omitted callback keys trigger replacement rather than being silently preserved.
• Exported and shared a single EMPTY_RESPONSE_ADS module constant between initialContext and chat.tsx, ensuring stable object reference identity when ads are disabled with zero allocation.
• Added unit test suite in cli/src/state/__tests__/message-block-store.test.ts verifying that subscribers fire only on true mutations, are suppressed on identical updates, and handle omitted keys in setCallbacks.

Test plan

[✓] bun test --config=/dev/null src/state/__tests__/message-block-store.test.ts — 4 pass, 0 fail
[✓] bun run --cwd cli typecheck — 0 errors
[✓] PR hygiene check passed

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch on the render-churn issue — setContext unconditionally created a new context object on every layout effect, and Immer/Zustand notified subscribers even when nothing changed. The guard logic is sound: setContext bails out via an early return inside the Immer producer when no updated key actually differs, and setCallbacks preserves 'full replacement' semantics by checking key-count first before comparing values, so omitted keys are correctly treated as changes. The EMPTY_RESPONSE_ADS constant is a nice touch to avoid a fresh {} allocation feeding into the equality check on every render.

A few things worth polishing before this lands:

  • The key-set comparison in setCallbacks only checks prevKeys.length !== nextKeys.length, not actual set equality. It happens to work because any differing key pair with equal counts still produces a value mismatch on the iterated key, but it's fragile — a small comment explaining why length-check + per-key diff is sufficient would help future readers, or just do a proper key-set diff.
  • Consider whether this shallow-equality pattern belongs in a shared helper (e.g. zustand/shallow or a local shallowEqual util) rather than being hand-rolled twice in this file, since other stores in cli/src/state likely have the same churn problem.
  • Tests are solid and cover the four cases described in the PR body — appreciated for a first-time contributor.

Overall this is a small, well-scoped, testable fix in cli/src/state and chat.tsx, not touching any restricted paths. Worth porting after a maintainer double-checks the key-set comparison edge case.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants